﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Demo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted; 
            webView.EnsureCoreWebView2Async();

        }

        private void WebView_CoreWebView2InitializationCompleted(object? sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
        {
             //注册csobj脚本c#互操作
             webView.CoreWebView2.AddHostObjectToScript("csobj", new ScriptCallbackObject());
             //注册全局变量csobj
             webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("var csobj = window.chrome.webview.hostObjects.csobj;");
             webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("var csobj_sync= window.chrome.webview.hostObjects.sync.csobj;");


            string rootPath = Environment.CurrentDirectory;
            string filepath = System.IO.Path.Combine(rootPath, "html", "index.html");
            Console.WriteLine(filepath);
            if (webView != null && webView.CoreWebView2 != null)
            {
                string text = System.IO.File.ReadAllText(filepath);
                webView.CoreWebView2.NavigateToString(text);
            }
        }
    }
}
